package com.meilai.project.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.system.UserDTO;
import com.meilai.project.entity.system.User;
import com.meilai.project.vo.app.MyInfoVO;
import com.meilai.project.vo.system.UserVO;
import com.meilai.project.vo.system.WebUserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author yxg
 * @date 2020/2/21 12:46
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("SELECT  " +
            "u.`name` AS `real_name`, " +
            "u.mobile, " +
            "d.head AS `company_name`, " +
            "d.`name` AS `company_abb_name` " +
            "FROM `user` u " +
            "LEFT JOIN ( " +
            "SELECT udr.user_id,querySecondLevelDepartmentId(udr.department_id) AS second_department_id " +
            "FROM user_department_relation udr " +
            "WHERE udr.main=1 " +
            ") ud ON u.id=ud.user_id " +
            "LEFT JOIN department d ON ud.second_department_id=d.id " +
            "WHERE u.id=#{user_id} AND u.deleted_at IS NULL ")
    MyInfoVO getAppMyInfo(@Param("user_id") Long user_id);

    @Select("SELECT u.*,udr.department_id AS main_department_id,udr.job AS main_department_job,d.name AS main_department_name " +
            "FROM `user` u  " +
            "LEFT JOIN user_department_relation udr ON u.id=udr.user_id AND udr.main=1 " +
            "LEFT JOIN department d ON d.id=department_id " +
            "WHERE u.id=#{id}")
    UserVO getUserVOById(@Param("id") Long id);

    //根据id返回管理员信息
    @Select("select * from user where id = #{id}")
    UserDTO selectUserDtoById(@Param("id") Long id);

    @Select("select * from user where username=#{username} and deleted_at is null")
    User getUserByUsername(String username);

    List<WebUserVO> selectPageList(Page<WebUserVO> page, @Param("name") String name,
                                   @Param("username") String username, @Param("role_id") Long role_id);

    List<User> getActiveUsers(@Param("ids") List<Long> ids);

    List<Long> getUserIdsByDepartmentIdsAndJobs(@Param(value = "departmentIds") List<Long> departmentIds, @Param(value = "jobs") List<String> jobs);

    List<Long> getSupperUserIdsByDepartmentIdAndJobs(@Param(value = "departmentId") Long departmentId, @Param(value = "jobs") List<String> jobs);

    List<Long> getUserIdsByDepartmentIdJob(@Param(value = "departmentId_job") List<String> departmentId_job);

    List<User> getUserListByRoleCode(@Param(value = "code") String code);

    List<Long> getUserResponsibleDeptIdByBusinessHead(@Param(value = "id") Long id);

    /**
     * 根据部门ID获取部门下所有用户信息（包括字部门）
     *
     * @param id
     * @return
     */
    List<User> getUserByDeptId(@Param(value = "id") Long id);

    List<User> getUserByUserMainDeptId(@Param(value = "id") Long id, @Param(value = "userId") Long userId);

    User userIsARole(@Param(value = "userId") Long userId, @Param(value = "code") String code);

    List<User> list_by_role_code(@Param(value = "code") String code);
}
